import React, { Component } from "react";
import { View, Alert } from "react-native";
import CustomHeader from "../../components/CustomHeader";
import { Input } from "react-native-elements";
import { request } from "../../utils";

export class UpdateName extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: "",
      nameError: ""
    };
  }

  submit = _ => {
    const { navigation } = this.props;
    const { name } = this.state;
    if (!name) {
      return;
    }

    request(
      "user/updateName",
      { name },
      userInfo => {
        global.userInfo = userInfo;
        navigation.getParam("getUserInfo")();
        navigation.goBack();
      },
      data => Alert.alert(data)
    );
  };

  render() {
    return (
      <View>
        <CustomHeader
          title="设置昵称"
          right="check"
          onRightClick={this.submit}
        />

        <Input
          placeholder="昵称"
          onChangeText={name => this.setState({ name })}
        />
      </View>
    );
  }
}

export default UpdateName;
